<!-- 添加或修改角色配置对话框 -->
<template>
    <div>
        <el-drawer title="关联员工" v-model="drawerShow" direction="rtl" :before-close="handleClose" size="60%"
            :close-on-click-modal="false">
            <div>
                <el-form :model="queryParams" ref="queryRef" :inline="true">
                    <el-form-item label="用户名称" prop="userName">
                        <el-input v-model="queryParams.userName" placeholder="请输入用户名称" clearable style="width: 180px" 
                            @keyup.enter="handleQuery" />
                    </el-form-item>
                    <el-form-item label="手机号码" prop="phonenumber">
                        <el-input v-model="queryParams.phonenumber" placeholder="请输入手机号码" clearable style="width: 180px" 
                            @keyup.enter="handleQuery" />
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
                    </el-form-item>
                </el-form>

                <el-row :gutter="10" class="mb8">
                    <el-col :span="1.5"> <el-button type="primary" plain icon="Plus" 
                        @click="openSelectUser" v-hasPermi="['system:role:add']" >添加用户</el-button> </el-col>
                </el-row>

                <el-table v-loading="loading" :data="userList">
                    <el-table-column type="index" label="#" width="55" align="center" />
                    <el-table-column label="用户名称" prop="userName" :show-overflow-tooltip="true" />
                    <el-table-column label="用户昵称" prop="nickName" :show-overflow-tooltip="true" />
                    <el-table-column label="邮箱" prop="email" :show-overflow-tooltip="true" />
                    <el-table-column label="手机" prop="phonenumber" :show-overflow-tooltip="true" />
                    <el-table-column label="状态" align="center" prop="status">
                        <template #default="scope">
                        <dict-tag :options="sys_normal_disable" :value="scope.row.status" />
                        </template>
                    </el-table-column>
                    <el-table-column label="创建时间" align="center" prop="createTime" width="180">
                        <template #default="scope">
                        <span>{{ parseTime(scope.row.createTime) }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                        <template #default="scope">
                            <el-button link type="primary" icon="CircleClose" 
                                @click="cancelAuthUser(scope.row)" v-hasPermi="['system:role:remove']">移 除</el-button>
                        </template>
                    </el-table-column>
                </el-table>

                <pagination v-show="total > 0" :total="total" 
                    v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" 
                    @pagination="getList" />

            </div>
        </el-drawer>
        <select-user ref="selectRef" :roleId="queryParams.roleId" @ok="handleQuery" />
    </div>
</template>
<script setup>
    import { ref, watch } from "vue"
    import { allocatedUserList, authUserCancel } from "@/api/system/role";
    import selectUser from "../selectUser";
    const props = defineProps(['show', 'roleId']);
    const emit = defineEmits(['update:show']);
    const { proxy } = getCurrentInstance();
    const { sys_normal_disable } = proxy.useDict("sys_normal_disable");
    const drawerShow = ref(false);
    const total = ref(0);
    const queryParams = ref({
        pageNum: 1,
        pageSize: 10,
        roleId: '',
        userName: undefined,
        phonenumber: undefined,
    });
    const loading = ref(false);
    const userList = ref([]);
    watch(() => props.show, () => {
        drawerShow.value = props.show;
        if(drawerShow.value){
            queryParams.value.roleId = props.roleId;
            if(props.roleId) {
                getList();
            }
        }
    }, { deep: true });
    const setDefaultFormData = (formData) => {
        
    };
    /** 搜索按钮操作 */
    const handleQuery = () => {
        queryParams.pageNum = 1;
        getList();
    }
    /** 重置按钮操作 */
    const resetQuery = () => {
        proxy.resetForm("queryRef");
        handleQuery();
    }
    /** 打开授权用户表弹窗 */
    const openSelectUser = () => {
        proxy.$refs["selectRef"].show();    
    }
    /** 取消授权按钮操作 */
    const cancelAuthUser = (row) => {
        proxy.$modal.confirm('确认要取消该用户"' + row.userName + '"角色吗？').then(function () {
            return authUserCancel({ userId: row.userId, roleId: queryParams.value.roleId });
        }).then(() => {
            getList();
            proxy.$modal.msgSuccess("取消授权成功");
        }).catch(() => {});
    }
    const getList = () => {
        loading.value = true;
        allocatedUserList(queryParams.value).then(response => {
            userList.value = response.rows;
            total.value = response.total;
            loading.value = false;
        });
    }
    const handleClose = () => {
        drawerShow.value = false;
        emit('update:show', false)
    };
</script>
<style scoped>
    :deep(.el-drawer__header){
        padding-bottom: 15px;
        margin-bottom: 0px!important;
        border-bottom: 1px solid #eee;
        font-weight: 700;
    }
</style>